<template>
  <div class="avue-contail" :class="{'avue--collapse':isCollapse}">
    <div class="avue-header">
      <!-- 顶部导航栏 -->
      <top ref="top" />
    </div>

    <div class="avue-layout">
      <div class="avue-left">
        <!-- 左侧导航栏 -->
        <sidebar />
      </div>
      <div class="avue-main" :class="{'avue-main--fullscreen':!isMenu}">
        <!-- 顶部标签卡 -->
        <tags />
        <transition name="fade-scale">
          <search class="avue-view" v-show="isSearch"></search>
        </transition>
        <!-- 主体视图层 -->
        <div
          style="height:100%;overflow-y:auto;overflow-x:hidden;"
          id="avue-view"
          v-show="!isSearch"
        >
          <keep-alive>
            <router-view class="avue-view" v-if="$route.meta.keepAlive" />
          </keep-alive>
          <router-view class="avue-view" v-if="!$route.meta.keepAlive" />
        </div>
      </div>
    </div>
    <div class="avue-shade" @click="showCollapse"></div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import tags from "./tags";
import search from "./search";
import top from "./top/";
import sidebar from "./sidebar/";
import admin from "@/util/admin";
import { getSession } from "@/util/auth";
export default {
  components: {
    top,
    tags,
    search,
    sidebar,
  },
  name: "index",
  provide() {
    return {
      index: this,
    };
  },
  data() {
    return {
      //搜索控制
      isSearch: false,
    };
  },
  mounted() {
    this.restoken();
    this.init();
  },
  computed: mapGetters(["isMenu", "isLock", "isCollapse", "website", "menu"]),
  props: [],
  methods: {
    showCollapse() {
      this.$store.commit("common/SET_COLLAPSE");
    },
    // 屏幕检测
    init() {
      this.$store.commit("common/SET_SCREEN", admin.getScreen());
      window.onresize = () => {
        setTimeout(() => {
          this.$store.commit("common/SET_SCREEN", admin.getScreen());
        }, 0);
      };
    },
    //打开菜单
    openMenu(item = {}) {
      this.$store.dispatch("user/GetMenu", item.parentId).then((data) => {
        if (data.length !== 0) {
          this.$router.$avueRouter.formatRoutes(data, true);
        }
        //当点击顶部菜单做的事件
        if (!this.validatenull(item)) {
          let itemActive = {},
            childItemActive = 0;
          //vue-router路由
          if (item.path) {
            itemActive = item;
          } else {
            if (this.menu[childItemActive].length == 0) {
              itemActive = this.menu[childItemActive];
            } else {
              itemActive = this.menu[childItemActive].children[childItemActive];
            }
          }
          this.$store.commit("user/SET_MENUID", item);
          this.$router.push({
            path: this.$router.$avueRouter.getPath(
              {
                name: itemActive.label,
                src: itemActive.path,
              },
              itemActive.meta
            ),
          });
        }
      });
    },
    // 定时刷新token
    restoken() {
      // 每1分钟检测是否快过期token 如果快过期了就更新token
      if (getSession("tokentime")) {
        setInterval(() => {
          if (new Date().valueOf() > Number(getSession("tokentime"))) {
            this.$store.dispatch("user/RefeshToken");
          }
        }, 1000 * 60);
      }
    },
  },
};
</script>